home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / packages / chistory.el < prev    next >
Encoding:
Text File  |  1995-03-25  |  6.6 KB  |  180 lines

  1. ;;; chistory.el --- list command history
  2.  
  3. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  4.  
  5. ;; Author: K. Shane Hartman
  6. ;; Maintainer: FSF
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Synched up with: FSF 19.28.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This really has nothing to do with list-command-history per se, but
  29. ;; its a nice alternative to C-x ESC (repeat-complex-command) and
  30. ;; functions as a lister if given no pattern.  It's not important
  31. ;; enough to warrant a file of its own.
  32.  
  33. ;;; Code:
  34.  
  35. (defvar command-history-map nil)
  36.  
  37. (defvar command-history-hook nil
  38.   "If non-nil, its value is called on entry to `command-history-mode'.")
  39.  
  40. ;;;###autoload
  41. (defun repeat-matching-complex-command (&optional pattern)
  42.   "Edit and re-evaluate complex command with name matching PATTERN.
  43. Matching occurrences are displayed, most recent first, until you
  44. select a form for evaluation.  If PATTERN is empty (or nil), every form
  45. in the command history is offered.  The form is placed in the minibuffer
  46. for editing and the result is evaluated."
  47.   (interactive "sRedo Command (regexp): ")
  48.   (if pattern
  49.       (if (equal (setq pattern
  50.                (substring pattern
  51.                   (or (string-match "[ \t]*[^ \t]" pattern)
  52.                       (length pattern))))
  53.          "")
  54.       (setq pattern nil)))
  55.   (let ((history command-history)
  56.     (temp)
  57.     (what))
  58.     (while (and history (not what))
  59.       (setq temp (car history))
  60.       (if (and (or (not pattern) (string-match pattern (symbol-name (car temp))))
  61.            (y-or-n-p (format "Redo %s? " (setq temp (prin1-to-string temp)))))
  62.       (setq what (car history))
  63.     (setq history (cdr history))))
  64.     (if (not what)
  65.     (error "Command history exhausted")
  66.       ;; Try to remove any useless command history element for this command.
  67.       (if (eq (car (car command-history)) 'repeat-matching-complex-command)
  68.       (setq command-history (cdr command-history)))
  69.       (edit-and-eval-command "Redo: " what))))
  70.  
  71. (defvar default-command-history-filter-garbage
  72.   '(command-history-mode
  73.     list-command-history
  74.     electric-command-history)
  75.   "*A list of symbols.  If `default-list-command-history-filter' is
  76. given a list whose car is an element of this list, then it will return
  77. non-nil (indicating the list should be discarded from the history).
  78. Initially, all commands related to the command history are discarded.")
  79.  
  80. (defvar list-command-history-filter 'default-command-history-filter
  81.   "If non-nil, should be the name of a function of one argument.
  82. It is passed each element of the command history when
  83. \\[list-command-history] is called.  If the filter returns non-nil for
  84. some element, that element is excluded from the history listing.  The
  85. default filter removes commands associated with the command-history.")
  86.  
  87. (defun default-command-history-filter (frob)
  88.   "Filter commands matching `default-command-history-filter-garbage' list
  89. from the command history."
  90.   (or (not (consp frob))
  91.       (memq (car frob) default-command-history-filter-garbage)))
  92.  
  93. (defvar list-command-history-max 32
  94.   "*If non-nil, should be a positive number which specifies the maximum
  95. length of the Command History listing produced by `list-command-history'.")
  96.  
  97. ;;;###autoload
  98. (defun list-command-history ()
  99.   "List history of commands typed to minibuffer.
  100. The number of commands listed is controlled by `list-command-history-max'.
  101. Calls value of `list-command-history-filter' (if non-nil) on each history
  102. element to judge if that element should be excluded from the list.
  103.  
  104. The buffer is left in Command History mode."
  105.   (interactive)
  106.   (with-output-to-temp-buffer
  107.       "*Command History*"
  108.     (let ((history command-history)
  109.       (buffer-read-only nil)
  110.       (count (or list-command-history-max -1)))
  111.       (while (and (/= count 0) history)
  112.     (if (and (boundp 'list-command-history-filter)
  113.          list-command-history-filter
  114.          (funcall list-command-history-filter (car history)))
  115.         nil
  116.       (setq count (1- count))
  117.       (prin1 (car history))
  118.       (terpri))
  119.     (setq history (cdr history))))
  120.     (save-excursion
  121.       (set-buffer "*Command History*")
  122.       (goto-char (point-min))
  123.       (if (eobp)
  124.       (error "No command history")
  125.     (Command-history-setup)))))
  126.  
  127. (defun Command-history-setup (&optional majormode modename keymap)
  128.   (set-buffer "*Command History*")
  129.   (use-local-map (or keymap command-history-map))
  130.   (lisp-mode-variables nil)
  131.   (set-syntax-table emacs-lisp-mode-syntax-table)
  132.   (setq buffer-read-only t)
  133.   (use-local-map (or keymap command-history-map))
  134.   (setq major-mode (or majormode 'command-history-mode))
  135.   (setq mode-name (or modename "Command History")))
  136.  
  137. (if command-history-map
  138.     nil
  139.   (setq command-history-map (make-sparse-keymap))
  140.   (set-keymap-parent command-history-map shared-lisp-mode-map)
  141.   (set-keymap-name command-history-map 'command-history-map)
  142.   (suppress-keymap command-history-map)
  143.   (define-key command-history-map "x" 'command-history-repeat)
  144.   (define-key command-history-map "\n" 'next-line)
  145.   (define-key command-history-map "\r" 'next-line)
  146.   (define-key command-history-map "\177" 'previous-line))
  147.  
  148. (defun command-history-repeat ()
  149.   "Repeat the command shown on the current line.
  150. The buffer for that command is the previous current buffer."
  151.   (interactive)
  152.   (save-excursion
  153.     (eval (prog1
  154.           (save-excursion
  155.         (beginning-of-line)
  156.         (read (current-buffer)))
  157.         (set-buffer
  158.          (car (cdr (buffer-list))))))))
  159.  
  160. ;;;###autoload
  161. (defun command-history-mode ()
  162.   "Major mode for examining commands from `command-history'.
  163. The number of commands listed is controlled by `list-command-history-max'.
  164. The command history is filtered by `list-command-history-filter' if non-nil.
  165. Use \\<command-history-map>\\[command-history-repeat] to repeat the command on the current line.
  166.  
  167. Otherwise much like Emacs-Lisp Mode except that there is no self-insertion
  168. and digits provide prefix arguments.  Tab does not indent.
  169. \\{command-history-map}
  170. Calls the value of `command-history-hook' if that is non-nil.
  171. The Command History listing is recomputed each time this mode is invoked."
  172.   (interactive)
  173.   (list-command-history)
  174.   (pop-to-buffer "*Command History*")
  175.   (run-hooks 'command-history-hook))
  176.  
  177. (provide 'chistory)
  178.  
  179. ;;; chistory.el ends here
  180.